package khleuven.mondzorg.app;

import khleuven.mondzorg.Logger;
import khleuven.mondzorg.R;
import khleuven.mondzorg.domain.DentalCareFacade;
import khleuven.mondzorg.domain.DentalCareFacadeImpl;
import khleuven.mondzorg.domain.Illustration;
import khleuven.mondzorg.domain.Illustrations;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.text.Html;
import android.text.SpannableStringBuilder;
import android.text.style.ClickableSpan;
import android.text.style.URLSpan;
import android.view.View;
import android.view.ViewGroup.LayoutParams;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

public class ActivityWithIllustrations extends ActivityWithSettingsAndHelp {
	
	protected DentalCareFacade dentalcare;
	protected Illustrations illustrations = null;
	private int illustrationindex = -1;
	
	private LinearLayout imageZone;
	private TextView imageDescriptionTxt;
	private ImageView image;
	private TextView imageNrTxt;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		dentalcare = new DentalCareFacadeImpl();
	}
	
    private void fetchViewElements() {
    	imageZone = (LinearLayout)findViewById(R.id.zone_Images);
    	imageDescriptionTxt = (TextView)findViewById(R.id.txt_desc_img);
    	image = (ImageView)findViewById(R.id.imageView);
    	imageNrTxt = (TextView)findViewById(R.id.txt_nr_img);	
    	
    	image.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, 
    			LayoutParams.WRAP_CONTENT, 0.70f));
	}

	protected void setIllustration(){
		fetchViewElements();
		Logger.logApp("setIllustration"); 
        int nrOfIllustrations = illustrations.getIllustrations().size();
        if(nrOfIllustrations > 0){
        	illustrationindex = 0;
//        	imageZone.setBackground(getResources().getDrawable(R.drawable.border));
//        	if(nrOfIllustrations>1){
//        		imageZone.setBackground(getResources().getDrawable(R.drawable.bordermultiple));
//        	}      		     
//        } else {
//        	imageZone.setBackground(null);
        }
    	setIllustrationFromIndex(); 
	}
	
	public void OnClickImageZone(View v){
		if(v.equals(imageZone)){
			setNextIllustration();
		}		
	}

	private void setNextIllustration() {
		Logger.logApp("setNextIllustration"); 
        int nrOfIllustrations = illustrations.getIllustrations().size();
        if(nrOfIllustrations > 1){
        	illustrationindex ++;
        	if(illustrationindex >= nrOfIllustrations){
        		illustrationindex = 0;
        	}
        	setIllustrationFromIndex();
        }
	}
	
	private void setIllustrationFromIndex(){
    	if(illustrationindex >= 0 && 
    	   illustrationindex < illustrations.getIllustrations().size()){
        	Illustration ill = illustrations.getIllustrations().get(illustrationindex);
        	Logger.logApp("setIllustrationFromIndex - ill_alt: "+ill.getAltTekst());
        	byte[] bb = ill.getBlob();
        	if(bb.length == 0){
        		imageDescriptionTxt.setText(ill.getAltTekst());
        	} else {
        		imageDescriptionTxt.setText("");
            	image.setImageBitmap(BitmapFactory.decodeByteArray(bb, 0, bb.length));
            	image.setContentDescription(ill.getAltTekst());
        	}
        	imageNrTxt.setText(illustrationindex+1+"/"+illustrations.getIllustrations().size());
    	}
    	else{
    		imageDescriptionTxt.setText("");
        	image.setImageBitmap(null);
        	image.setContentDescription("");
        	imageNrTxt.setText(getResources().getText(R.string.img_nrs));
    	}
    }
	
	protected void setTextViewHTML(TextView text, String html){
	    CharSequence sequence = Html.fromHtml(html);
	    SpannableStringBuilder strBuilder = new SpannableStringBuilder(sequence);
	    URLSpan[] urls = strBuilder.getSpans(0, sequence.length(), URLSpan.class);   
	    for(URLSpan span : urls) {
	    	Logger.logApp("url: "+span.getURL());
	        makeLinkClickable(strBuilder, span);
	    }
	    text.setText(strBuilder);       
	}
	
	protected void makeLinkClickable(SpannableStringBuilder strBuilder, final URLSpan span){
		// do nothing, this is handled by the sub-activities
	}
	
	protected void makeLinkClickable(SpannableStringBuilder strBuilder, final URLSpan span, ClickableSpan clickable)	{
	    int start = strBuilder.getSpanStart(span);
	    int end = strBuilder.getSpanEnd(span);
	    int flags = strBuilder.getSpanFlags(span);
	    strBuilder.setSpan(clickable, start, end, flags);
	    strBuilder.removeSpan(span);
	}
	
	
}
